/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIv2;

import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JOptionPane;

import jsonObjects.Profile;
import serverHandling.SessionObjects;

public class VersioningReplacePanel extends javax.swing.JPanel {
    private static final long serialVersionUID = 7245964797377946859L;
    public VersioningReplacePanel() {
        initComponents();
    }

    @SuppressWarnings("serial")
    private void initComponents() {
        List<Profile> activProfTab = SessionObjects.getProfiles();
        Object[][] futureMenuItems;
        if (activProfTab != null) {
            futureMenuItems = new Object[activProfTab.size()][activProfTab
                    .size()];
            for (int i = 0; i < activProfTab.size(); i++) {
                futureMenuItems[i][0] = activProfTab.get(i).getName();
            }
        } else
            futureMenuItems = new Object[0][0];

        versioningReplacePanel = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTable5 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        jTable5.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Profile"
                }
        ){
        boolean[] canEdit = new boolean [] {
                false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable5.getColumnModel().getColumn(0).setResizable(false);

        jScrollPane5.setViewportView(jTable5);

        jButton1.setText("Next");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        javax.swing.GroupLayout versioningReplacePanelLayout = new javax.swing.GroupLayout(versioningReplacePanel);
        versioningReplacePanel.setLayout(versioningReplacePanelLayout);
        versioningReplacePanelLayout.setHorizontalGroup(
                versioningReplacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(versioningReplacePanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(versioningReplacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, versioningReplacePanelLayout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(jButton1)))
                                .addContainerGap())
        );
        versioningReplacePanelLayout.setVerticalGroup(
                versioningReplacePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(versioningReplacePanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton1)
                                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(versioningReplacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(versioningReplacePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
        );
    }

    private void jButton1ActionPerformed(MouseEvent evt) {
        ProfileFileListGUI replacePanel = null;
        if (jTable5.getSelectedRowCount() == 1) {

            int row = jTable5.getSelectedRow();
            int column = jTable5.getSelectedColumn();
            String urObjctInCell = (String) jTable5.getValueAt(row, column);
            final Profile profTmp = SessionObjects
                    .getProfileByName(urObjctInCell);

            if (profTmp == null)
                JOptionPane.showMessageDialog(null,
                        "Error while loading files!");

            else {
                replacePanel = new ProfileFileListGUI(profTmp);
            }

        } else {
            JOptionPane.showMessageDialog(null, "Choose profile");
        }

        javax.swing.JPanel parenPanel = (javax.swing.JPanel) versioningReplacePanel.getParent();
        parenPanel.removeAll();

        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable5;
    private javax.swing.JPanel versioningReplacePanel;
}
